package edu.wit.acl.controller;

import com.alibaba.fastjson.JSONObject;

import edu.wit.acl.service.IndexService;
import edu.wit.common.annotation.Log;
import edu.wit.common.enums.BusinessType;
import edu.wit.common.result.R;
import edu.wit.common.security.service.TokenService;
import edu.wit.common.utils.ServletUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.access.prepost.PreAuthorize;
//import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/acl/index")
//@CrossOrigin
@Api(tags = "index模块")
public class IndexController extends BaseController{

    @Autowired
    private IndexService indexService;

    @Autowired
    private TokenService tokenService;


    /**
     * 根据token获取用户信息
     */
    @ApiOperation(value = "获取用户信息")
//    @Log(title = "获取用户信息", businessType = BusinessType.INSERT)
    @GetMapping("info")
    public R info() {
        //获取当前登录用户用户名
        String token = ServletUtils.getRequest().getHeader("token");
        String username = tokenService.getLoginUser(token).getUsername();

        Map<String, Object> userInfo = indexService.getUserInfo(username);
        return R.ok().data(userInfo);
    }

    /**
     * 获取菜单
     *
     * @return
     */
    @ApiOperation(value = "获取用户菜单")
//    @Log(title = "获取用户菜单", businessType = BusinessType.INSERT)
    @GetMapping("menu")
    public R getMenu() {
        //获取当前登录用户用户名
//        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        String token = ServletUtils.getRequest().getHeader("token");
        String username = tokenService.getLoginUser(token).getUsername();
        System.out.println("---------------------------------------------------------------------------------menu");
        List<JSONObject> permissionList = indexService.getMenu(username);
        return R.ok().data("permissionList", permissionList);
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("logout")
//    @Log(title = "退出登录", businessType = BusinessType.INSERT)
    public R logout() {
        return R.ok();
    }

    @ApiOperation(value = "获取用户劳务公司")
    @GetMapping("getUserCorpId")
    public R getUserCorpId() {
        //获取当前登录用户用户名
//        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        String username = tokenService.getLoginUser(ServletUtils.getRequest()).getUsername();
        String corpId = roleService.selectCorpIdByUserId(userService.selectByUsername(username).getId());
        return R.ok().data("data",corpId);
    }

    /**
     * 昊广首页获取租户数量
     *
     * @return
     */
    @ApiOperation(value = "昊广首页获取租户数量")
    @Log(title = "获取用户菜单", businessType = BusinessType.SELECT)
    @GetMapping("getTenantCounts")
    public R getTenantCounts() {
        //获取当前登录用户用户名
        return R.ok().data("tenantNums", userService.selectTenantNums());
    }
}
